package com.shenhan.esactionjd.utils;

import com.shenhan.esactionjd.pojo.Content;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shenhan
 * @create 2020/5/20 17:23
 * @description
 */
public class HtmlParseUtil {
    public static List<Content> parseJD(String keywords) throws Exception {
        String url = "https://search.jd.com/Search?keyword=" + keywords;
        final Document document = Jsoup.parse(new URL(url), 30000);
        final Element goodsElement = document.getElementById("J_goodsList");
        final Elements elements = goodsElement.getElementsByTag("li");
        final ArrayList<Content> goodsList = new ArrayList<>();
        for (Element element : elements) {
            final String img = element.getElementsByTag("img").eq(0).attr("src");
            final String price = element.getElementsByClass("p-price").eq(0).text();
            final String title = element.getElementsByClass("p-name").eq(0).text();
            Content content = new Content(title, img, price);
            goodsList.add(content);
        }
        return goodsList;
    }


    @SneakyThrows
    public static void main(String[] args) {
        new HtmlParseUtil().parseJD("docker").forEach(System.out::println);
        /*// https://search.jd.com/Search?keyword=java
        String url = "https://search.jd.com/Search?keyword=java";
        // 解析网页 js对象
        final Document document = Jsoup.parse(new URL(url), 30000);
        // 所有在js中可以使用的方法在这里都能用
        final Element goodElement = document.getElementById("J_goodsList");
        // 获取所有的li标签
        final Elements elements = goodElement.getElementsByTag("li");
        // 获取元素中的内容
        for (Element element : elements) {
            final String img = element.getElementsByTag("img").eq(0).attr("src");
            final String price = element.getElementsByClass("p-price").eq(0).text();
            final String title = element.getElementsByClass("p-name").eq(0).text();

            System.out.println("========");
            System.out.println(img);
            System.out.println(price);
            System.out.println(title);
        }*/
    }


}
